package org.ichow.scenegame.controller
{
	import flash.display.DisplayObject;
	import org.ichow.scenegame.core.NotiFactory
	import org.ichow.scenegame.events.NotiEvent;
	import org.ichow.scenegame.model.AssetsProxy;
	import org.ichow.scenegame.notis.BaseNoti;
	import org.ichow.scenegame.view.NotiMediator;
	import org.ichow.scenegame.view.SceneMediator;
	import org.ichow.scenegame.view.ToolsBarMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * ...
	 * 控制层工厂
	 * 根据不同的状态调用素材代理返回显示对象
	 * @author M.ichow
	 */
	public class NotiCommand extends SimpleCommand
	{
		
		override public function execute( notification:INotification ):void
		{
			super.execute( notification );
			// 当前状态
			var status:String = notification.getType();
			// 控制层容器
			var box:NotiFactory = notification.getBody() as NotiFactory;
			var params:Object = {};
			
			if ( !box.hasNoti( status ) )
			{
				// 素材代理
				var assets:AssetsProxy = facade.retrieveProxy( AssetsProxy.NAME ) as AssetsProxy;
				var scene:SceneMediator = facade.retrieveMediator( SceneMediator.NAME ) as SceneMediator;
				var notiSkin:DisplayObject;
				
				var w:int = int( scene.getValueForKey( "width" ) );
				var h:int = int( scene.getValueForKey( "height" ) );
				
				notiSkin = assets.getSkin( box.getSkinName( status ) );
				
				switch ( status )
				{
					case NotiEvent.INIT: 
						params = { x: Math.floor( w - notiSkin.width - 20 ) , y: Math.floor( h - notiSkin.height - 20 ) };
						break;
					case NotiEvent.PLAY: 
						params = { x: Math.floor( w - notiSkin.width ) / 2 , y: 16 + Math.floor( h - notiSkin.height ) / 2 , item: assets.getClass( "Item" ) , data: assets.getItems() };
						break;
					case NotiEvent.READY: 
					case NotiEvent.CONTINUE: 
						params = { x: Math.floor( w - notiSkin.width ) / 2 , y: 16 + Math.floor( h - notiSkin.height ) / 2 };
						break;
					case NotiEvent.END: 
						params = { x: Math.floor( w - notiSkin.width ) / 2 , y: 16 + Math.floor( h - notiSkin.height ) / 2 , item: assets.getClass( "Way" ) };
						break;
				}
				// 储存 & 初始化
				box.setNoti( status , notiSkin );
				box.update( status , params );
			}
			
			switch ( status )
			{
				case NotiEvent.CONTINUE: 
					var noti:NotiMediator = facade.retrieveMediator( NotiMediator.NAME ) as NotiMediator;
					params = { value: noti.continueInfo , result: noti.result };
					box.update( status , params );
					break;
				case NotiEvent.END: 
					var tools:ToolsBarMediator = facade.retrieveMediator( ToolsBarMediator.NAME ) as ToolsBarMediator;
					params = { toolsInfo: tools.getToolsInfo() , ways: box.ways };
					box.update( status , params );
					break;
			}
			
			box.active( status );
		}
	
	}

}